jQuery(document).ready(function(){
	//jQuery("h3.product-name").text($(this).text().substr(0, 37)+'...');
	var current_pos = getCookie("fb_current_pos");
		displayDefaultDevices();
	if (current_pos > 1) {
		console.log(current_pos + "TRUE");
		if(current_pos=="2") {
			jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
			jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
			jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		
			jQuery(".deviceInfos").hide();
			jQuery(".deviceInfosIns").show();
			
			var mode = getCookie("fb_mode");
			var gadget = getCookie("fb_gadget");
			var price = getCookie("fb_price");
		  
			if(mode!="edit"){      
				jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox, .storeBox").removeClass("active");
				jQuery('#pname, #pemail, #pdob, #padd').hide();
				jQuery('#promo, #commitmentBox, #mobtel, #globestore').text("None");
				jQuery('#gadget').text(gadget + " - " + price);
		    }
		}
		if(current_pos=="3") {
			jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
			jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
			jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");

			jQuery(".deviceInfos").hide();
			jQuery(".deviceInfosIns").show();
			
			var mode = getCookie("fb_mode");
			var gadget = getCookie("fb_gadget");
			var price = getCookie("fb_price");
			var promo = getCookie("fb_promo");
			var commitmentBox = getCookie("fb_commitmentBox");
		  
			if(mode!="edit"){      
				jQuery(".prepaidBox, .profileBox, .storeBox").removeClass("active");
				jQuery('#pname, #pemail, #pdob, #padd').hide();
				jQuery('#mobtel, #globestore').text("None");
				jQuery('#gadget').text(gadget + " - " + price);
				jQuery('#promo').html(promo);
				jQuery('#commitmentBox').text(commitmentBox);
			}
		}
		if(current_pos=="4") {
			jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
			jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
			jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
			
			jQuery(".deviceInfos").hide();
			jQuery(".deviceInfosIns").show();
			
			var mode = getCookie("fb_mode");
			var gadget = getCookie("fb_gadget");
			var price = getCookie("fb_price");
			var promo = getCookie("fb_promo");
			var commitmentBox = getCookie("fb_commitmentBox");
			var mobtel = getCookie("fb_mobtel");
		  
			if(mode!="edit"){      
				jQuery(".profileBox, .storeBox").removeClass("active");
				jQuery('#pname, #pemail, #pdob, #padd').hide();
				jQuery('#globestore').text("None");
				jQuery('#gadget').text(gadget + " - " + price);
				jQuery('#promo').html(promo);
				jQuery('#commitmentBox').text(commitmentBox);
				jQuery('#mobtel').text(mobtel);
			}
		}
		if(current_pos=="5") {
			jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
			jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
			jQuery(".deviceInfos").hide();
			jQuery(".deviceInfosIns").show();
			
			var mode = getCookie("fb_mode");
			var gadget = getCookie("fb_gadget");
			var price = getCookie("fb_price");
			var promo = getCookie("fb_promo");
			var commitmentBox = getCookie("fb_commitmentBox");
			var mobtel = getCookie("fb_mobtel");
			var username = getCookie("fb_username");
			var eadd = getCookie("fb_eadd");
			var bday = getCookie("fb_bday");
			var address = getCookie("fb_address");
		  
			if(mode!="edit"){      
				jQuery(".storeBox").removeClass("active");
				jQuery(".pnone").hide();
				jQuery('#globestore').text("None");
				jQuery('#gadget').text(gadget + " - " + price);
				jQuery('#promo').html(promo);
				jQuery('#commitmentBox').text(commitmentBox);
				jQuery('#mobtel').text(mobtel);
				jQuery('#name').text(username);
				jQuery('#email').text(eadd);
				jQuery('#dob').text(bday);
				jQuery('#add').text(address);
				jQuery("#pname, #pemail, #pdob, #padd").show();
			}
		}
	} else {
		console.log(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	}
	
		$("select.price option").eq(0).attr("value","price-range1"); // all
		$("select.price option").eq(1).attr("value","price-range2"); // 0 - 2000
		$("select.price option").eq(2).attr("value","price-range3"); // 20001 - 30001
		$("select.price option").eq(3).attr("value","price-range4"); // 
		$("select.price option").eq(4).attr("value","price-range5"); //
		$("select.price option").eq(5).attr("value","price-range6");

	var price = $("select.price").val();
  	$("select").change(function(){
		orderDevices();
	});
 
	jQuery("#btn_top").click(function() {
		FB.Canvas.scrollTo(0,0);
	});
}); // . . . . . . End Document Ready

function orderDevices(){
  // reset
  $("ul.gallery-products li.item").removeClass("_hide");
  $("ul.gallery-products li.item").removeClass("_display");
  $("ul.gallery-products li.item").removeAttr("style");
  // reset

  var price = "All", brand = "All", itemIn = 0;
  brand = $("select.brand").val();

        var price = $("select.price").val();
        $("ul.gallery-products li.item").each(function(){
        	 
            $(this).removeAttr("style");               
            if(brand == "All" || brand == $(this).find("span.brand").text()){ 
                 $(this).removeAttr("style");               
        		 alert(price+" price brand "+brand);
                 $(this).attr("style","display:block;");                
              	  
                  switch(price){
                  	case "price-range1":
                  		$(this).addClass("_display"); 
                  		alert(price);
                  	break; 
                    case "price-range2": 
                      /* . . . This will take the price string and parse to Float . . . */
                      var p = $(this).find("span.price").text(); var str = p.replace(/Php/, " "); str = p.replace(/P/, " ");
                      pr = parseFloat(str.trim()); 
                      /* . . . This will take the price and parse to Float . . . */
                      if(pr <= 2000.00){                      	 
                        $(this).addClass("_display"); 
                        itemIn++;
                      } 
                      else{
                      	$(this).removeAttr("style");    
                        $(this).addClass("_hide");             
                      }  
                    break;
                    case "price-range3":
                      var p = $(this).find("span.price").text(); var str = p.replace(/Php/, " "); str = p.replace(/P/, " ");
                      pr = parseFloat(str.trim());
                  
                      if( pr  <= 3000.00 && pr >= 2001.00){
                        $(this).addClass("_display"); itemIn++; 	   
                      } 
                      else{
                      	$(this).removeAttr("style");    
                        $(this).addClass("_hide");             
                      }                  
                    break;
                    case "price-range4": 
                      var p = $(this).find("span.price").text(); var str = p.replace(/Php/, " "); str = p.replace(/P/, " ");
                      pr = parseFloat(str.trim());
                  
                      if( pr  <= 4000.00 && pr >= 3001.00){
                        $(this).addClass("_display"); itemIn++;  	       
                      } 
                      else{
                      	$(this).removeAttr("style");    
                        $(this).addClass("_hide");             
                      }                  
                   break;
                   case "price-range5": 
                    var p = $(this).find("span.price").text(); var str = p.replace(/Php/, " "); str = p.replace(/P/, " ");
                    pr = parseFloat(str.trim());
                
                    if( pr  <= 5000.00 && pr >= 4001.00){               
                      $(this).addClass("_display");itemIn++;	 
                    }           
                    else{
                      $(this).removeAttr("style");    
                      $(this).addClass("_hide");             
                    }                  
                   break;
                   case "price-range6":
                    var p = $(this).find("span.price").text(); var str = p.replace(/Php/, " "); str = p.replace(/P/, " ");
                    pr = parseFloat(str.trim());
                
                    if( pr  <= 9000.00 && pr >= 5001.00){           
                      $(this).addClass("_display");  itemIn++; 	 
                    } 
                    else{
                      $(this).removeAttr("style");    
                      $(this).addClass("_hide");             
                    }                  
                   break;
                   default:
                  		$(this).removeAttr("style");     
                		$(this).addClass("_hide"); 
                 		$(this).hide(); 
                   break; 

                } //switch price      
            }else{
                 $(this).removeAttr("style");     
                 $(this).addClass("_hide"); 
                 $(this).hide(); 
            } 
        });
  		$("._hide").hide();          
 
    } // end orderDevices

function lastPostFunc(){
    
    jQuery("ul.gallery-products li:hidden:lt(5):not(._hide)").show();  //less than 10 so nine  
    jQuery("ul.gallery-products li._display:lt(5):not(._hide)").show();      
    // adjust height ng iframe
    resizeIframe();

} // end lastPostFunc

function displayDefaultDevices(){	
 	 
    $("li.item").each(function(index, value){ 
        if( index > 5 ){
           jQuery(this).hide();
        }
    });   
  
}

function getCookie(fb_cname) {
    var name = fb_cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) 
    {
      var c = ca[i].replace(/\s/g, "");
      if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}  

function getCookie(fb_name) {
    var nameEQ = fb_name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++){
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/* . . .  Todo List  . . . /
.	
.	1. Display default number of phones
.	2. Load more button
.
.
.
.
* . . . . . . . . . . . . */